/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.account.Key.UCServiceBy;
/*     */ import com.zimbra.common.localconfig.DebugConfig;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.UCService;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.admin.message.CountObjectsRequest;
/*     */ import com.zimbra.soap.admin.message.CountObjectsResponse;
/*     */ import com.zimbra.soap.admin.type.CountObjectsType;
/*     */ import com.zimbra.soap.admin.type.DomainSelector;
/*     */ import com.zimbra.soap.admin.type.DomainSelector.DomainBy;
/*     */ import com.zimbra.soap.admin.type.UCServiceSelector;
/*     */ import com.zimbra.soap.admin.type.UCServiceSelector.UCServiceBy;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.concurrent.atomic.AtomicLong;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CountObjects
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  51 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  52 */     CountObjectsRequest req = (CountObjectsRequest)JaxbUtil.elementToJaxb(request);
/*  53 */     CountObjectsType countObjectsType = req.getType();
/*     */     
/*  55 */     if (countObjectsType == null) {
/*  56 */       throw ServiceException.INVALID_REQUEST("No type specified", null);
/*     */     }
/*  58 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*  60 */     UCService ucService = null;
/*  61 */     UCServiceSelector ucserviceSelector = req.getUcService();
/*  62 */     if (null != ucserviceSelector) {
/*  63 */       if (!countObjectsType.allowsUCService()) {
/*  64 */         throw ServiceException.INVALID_REQUEST("UCService cannot be specified for type: " + countObjectsType.name(), null);
/*     */       }
/*     */       
/*  67 */       String value = ucserviceSelector.getKey();
/*  68 */       ucService = prov.get(Key.UCServiceBy.fromString(ucserviceSelector.getBy().name()), value);
/*  69 */       if (ucService == null) {
/*  70 */         throw AccountServiceException.NO_SUCH_UC_SERVICE(value);
/*     */       }
/*     */     }
/*     */     
/*  74 */     List<DomainSelector> specifiedDomains = req.getDomains();
/*  75 */     if ((!countObjectsType.allowsDomain()) && (!specifiedDomains.isEmpty())) {
/*  76 */       throw ServiceException.INVALID_REQUEST("domain cannot be specified for type: " + countObjectsType.name(), null);
/*     */     }
/*     */     
/*     */ 
/*  80 */     long count = 0L;
/*  81 */     if ((specifiedDomains.isEmpty()) && (!zsc.getAuthToken().isAdmin()) && (countObjectsType.allowsDomain()) && (!countObjectsType.equals(CountObjectsType.domain)))
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/*  86 */       List<Domain> domains = prov.getAllDomains();
/*  87 */       AdminAccessControl aac = AdminAccessControl.getAdminAccessControl(zsc);
/*  88 */       AdminRight associatedRight = getAssociatedRight(countObjectsType);
/*  89 */       for (Iterator<Domain> it = domains.iterator(); it.hasNext();) {
/*  90 */         Domain domain = (Domain)it.next();
/*  91 */         if (!aac.hasRight(domain, associatedRight)) {
/*  92 */           it.remove();
/*     */         }
/*     */       }
/*  95 */       count = 0L;
/*  96 */       int threshold = DebugConfig.minimumDomainsToUseThreadsForDomainAdminCountObjects;
/*  97 */       if ((threshold > 0) && (domains.size() >= threshold))
/*     */       {
/*     */ 
/* 100 */         AtomicLong atomicCount = new AtomicLong(0L);
/* 101 */         List<Thread> threads = Lists.newArrayList();
/* 102 */         int chunkSize = domains.size() / DebugConfig.numberOfThreadsToUseForDomainAdminCountObjects + 1;
/* 103 */         int lastIndex = domains.size() - 1;
/* 104 */         int begin = 0;
/* 105 */         int end = lastIndex < chunkSize ? lastIndex : chunkSize - 1;
/* 106 */         while (end <= lastIndex) {
/* 107 */           threads.add(new Thread(new GetDomainCountsThread(atomicCount, prov, domains.subList(begin, end + 1), countObjectsType, ucService, null), String.format("%s-CountsForDomains-%d", new Object[] { Thread.currentThread().getName(), Integer.valueOf(threads.size()) })));
/*     */           
/*     */ 
/* 110 */           if (end >= lastIndex) {
/*     */             break;
/*     */           }
/* 113 */           begin += chunkSize;
/* 114 */           end += chunkSize;
/* 115 */           if (end > lastIndex) {
/* 116 */             end = lastIndex;
/*     */           }
/*     */         }
/* 119 */         for (Thread thread : threads) {
/* 120 */           thread.start();
/*     */         }
/* 122 */         for (Thread thread : threads) {
/*     */           try {
/* 124 */             thread.join();
/*     */           } catch (InterruptedException e) {
/* 126 */             ZimbraLog.search.debug("Unexpected exception counting for domain", e);
/*     */           }
/*     */         }
/* 129 */         count = atomicCount.get();
/*     */       } else {
/* 131 */         for (Domain domain : domains) {
/* 132 */           count += prov.countObjects(countObjectsType, domain, ucService);
/*     */         }
/*     */       }
/* 135 */     } else if ((!specifiedDomains.isEmpty()) && (countObjectsType.allowsDomain()))
/*     */     {
/* 137 */       for (DomainSelector specifiedDomain : specifiedDomains) {
/* 138 */         DomainSelector.DomainBy by = specifiedDomain.getBy();
/* 139 */         String domValue = specifiedDomain.getKey();
/* 140 */         Domain domain = prov.get(Key.DomainBy.fromString(by.name()), domValue);
/* 141 */         if (domain == null) {
/* 142 */           throw AccountServiceException.NO_SUCH_DOMAIN(domValue);
/*     */         }
/* 144 */         checkDomainRight(zsc, domain, getAssociatedRight(countObjectsType));
/* 145 */         count += prov.countObjects(countObjectsType, domain, ucService);
/*     */       }
/*     */     }
/*     */     else {
/* 149 */       checkRight(zsc, context, null, getAssociatedRight(countObjectsType));
/* 150 */       count += prov.countObjects(countObjectsType, null, ucService);
/*     */     }
/*     */     
/* 153 */     return zsc.jaxbToElement(new CountObjectsResponse(count, countObjectsType.name()));
/*     */   }
/*     */   
/*     */   private class GetDomainCountsThread
/*     */     implements Runnable
/*     */   {
/*     */     private final AtomicLong atomicCount;
/*     */     private final Provisioning prov;
/*     */     private final List<Domain> domains;
/*     */     private final CountObjectsType countObjectsType;
/*     */     private final UCService ucService;
/*     */     
/*     */     private GetDomainCountsThread(Provisioning atomicCount, List<Domain> prov, CountObjectsType domains, UCService countObjectsType)
/*     */     {
/* 167 */       this.atomicCount = atomicCount;
/* 168 */       this.prov = prov;
/* 169 */       this.domains = domains;
/* 170 */       this.countObjectsType = countObjectsType;
/* 171 */       this.ucService = ucService;
/*     */     }
/*     */     
/*     */     public void run()
/*     */     {
/* 176 */       for (Domain domain : this.domains) {
/*     */         try {
/* 178 */           this.atomicCount.addAndGet(this.prov.countObjects(this.countObjectsType, domain, this.ucService));
/*     */         } catch (ServiceException e) {
/* 180 */           ZimbraLog.search.debug("Problem counting %s for domain %s", this.countObjectsType, domain, e);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 188 */     relatedRights.add(Rights.Admin.R_countAccount);
/* 189 */     relatedRights.add(Rights.Admin.R_countAlias);
/* 190 */     relatedRights.add(Rights.Admin.R_countDomain);
/* 191 */     relatedRights.add(Rights.Admin.R_countDistributionList);
/* 192 */     relatedRights.add(Rights.Admin.R_countCos);
/* 193 */     relatedRights.add(Rights.Admin.R_countServer);
/* 194 */     relatedRights.add(Rights.Admin.R_countCalendarResource);
/*     */   }
/*     */   
/*     */   private AdminRight getAssociatedRight(CountObjectsType countObjectsType) {
/* 198 */     switch (countObjectsType) {
/*     */     case account: 
/*     */     case accountOnUCService: 
/*     */     case internalArchivingAccount: 
/*     */     case internalUserAccount: 
/*     */     case userAccount: 
/* 204 */       return Rights.Admin.R_countAccount;
/*     */     case alias: 
/* 206 */       return Rights.Admin.R_countAlias;
/*     */     case calresource: 
/* 208 */       return Rights.Admin.R_countCalendarResource;
/*     */     case cos: 
/*     */     case cosOnUCService: 
/* 211 */       return Rights.Admin.R_countCos;
/*     */     case dl: 
/* 213 */       return Rights.Admin.R_countDistributionList;
/*     */     case domain: 
/*     */     case domainOnUCService: 
/* 216 */       return Rights.Admin.R_countDomain;
/*     */     case server: 
/* 218 */       return Rights.Admin.R_countServer;
/*     */     }
/* 220 */     return null;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/CountObjects.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */